#
# Created By: Kevin T. Ryan
# Created On: 2007-Mar-01 @ 10:49:06 PM
# Email:      kevin.t.ryan@gmail.com
# License:    BSD (http://www.opensource.org/licenses/bsd-license.php)
#
# Defines one class, ColorPalette that allows one to put as many
# colors that they want into the container and then allows our
# graphing library (graphn) to iterate through the colors for
# each new dataset (eg, so that different lines stand out from
# each other).
#

class ColorPalette(object):

    def __init__(self, *colors):
        '''Create a color palette.  Arguments should be either lists (ie, rgb) or integers (ie, gd allocated colors).'''
        self._colors  = [color for color in colors]
        self._current = 0

    def next(self):
        '''Returns the next color from the palette.'''
        if self._current < 0 or not self._colors:
            raise ValueError, "No colors in the palette."
        col = self._colors[self._current]
        if self._current + 1 >= len(self._colors):
            self._current = 0
        else:
            self._current += 1
        return col

    def update_last_index(self, newvalue):
        '''Updates the most recently provided color to 'newvalue'.

        You might want to do this, for example, if you allocated
        the color in gd and didn't want to keep on reallocating
        the same color ... just use the allocated one.
        '''
        return
        if self._current < 0 or not self._colors:
            raise ValueError, "No colors in the palette."
        if self._current == 0:
            prev_idx = len(self._colors) - 1
        else:
            prev_idx = self._current - 1

        self._colors[prev_idx] = newvalue
        return True

    def reset(self):
        '''Resets the ColorPalette so that the next color you get is the first in the scheme.'''
        self._current = 0
        return True
